package com.alibaba.citrus.ots.application.report.ability.facade.service;

import com.alibaba.citrus.ots.application.report.ability.repository.CustomerStatementReportReadRepository;
import com.alibaba.citrus.ots.common.annotation.FacadeInvoker;
import com.alibaba.citrus.ots.common.exception.FacadeException;
import com.alibaba.cz.base.tool.message.ErrorMessage;
import com.alibaba.cz.base.tool.page.PageResult;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.epoch.app.crplatformenhance.api.customersalescopequery.dto.QuerySaleScopeByIdRequest;
import com.epoch.app.crplatformenhance.api.customersalescopequery.service.CustomerSaleScopeQueryService;
import com.epoch.app.crplatformenhance.sdo.CustomerSaleScopeSDO;
import com.epoch.app.otsapplicationreportsdk.customerstatementread.service.CustomerStatementReadService;
import com.epoch.app.otsapplicationreportsdk.model.dto.CustomerStatementReportSDO;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryCustomerSaleScopeListByLoginUserRequest;
import com.epoch.app.otsapplicationreportsdk.model.dto.QueryCustomerStatementReportRequest;
import com.epoch.app.otsapplicationreportsdk.service.OtsApplicationReportSdkService;
import com.google.common.collect.Lists;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zhoubc
 * @description: TODO
 * @date 2023/9/28 17:26
 */
@Primary
@Component
public class CustomerStatementReadServiceImpl implements CustomerStatementReadService {
    //private static Log log = Log.getLogger(CustomerStatementReadServiceImpl.class);

    @Resource
    CustomerStatementReportReadRepository customerStatementReportReadRepository;

    @Resource
    CustomerSaleScopeQueryService customerSaleScopeQueryService;

    @Resource
    OtsApplicationReportSdkService otsApplicationReportSdkService;

    @Override
    @FacadeInvoker
    public Result<List<CustomerStatementReportSDO>> pageQueryCustomerStatementList(QueryCustomerStatementReportRequest queryCustomerStatementReportRequest) {
        if (queryCustomerStatementReportRequest.getQueryDate() == null) {
            throw new FacadeException(ErrorMessage.code("OTS-01-010-01-15-013"));
        }

        //数据权限
        List<String> queryCustomerCodes = CollectionUtils.isNotEmpty(queryCustomerStatementReportRequest.getCustomerCodes())?
                queryCustomerStatementReportRequest.getCustomerCodes(): Lists.newArrayList();
        if (StringUtils.isNotBlank(queryCustomerStatementReportRequest.getCustomerId())) {
            QuerySaleScopeByIdRequest querySaleScopeByIdRequest = QuerySaleScopeByIdRequest.builder().id(queryCustomerStatementReportRequest.getCustomerId()).build();
            Result<CustomerSaleScopeSDO> customerSaleScopeResult = customerSaleScopeQueryService.querySaleScopeById(querySaleScopeByIdRequest);
            if (customerSaleScopeResult == null || Objects.isNull(customerSaleScopeResult.getResult())) {
                throw new FacadeException(ErrorMessage.code("OTS-01-010-01-15-015"));
            }
            queryCustomerCodes.add(customerSaleScopeResult.getResult().getCustomerCode());
        }else{
            Result<List<CustomerSaleScopeSDO>> saleScopeResult = otsApplicationReportSdkService.queryCustomerSaleScopeListByLoginUser(new QueryCustomerSaleScopeListByLoginUserRequest());
            if (saleScopeResult != null  && CollectionUtils.isNotEmpty(saleScopeResult.getResult())) {
                List<String> customerCodes = saleScopeResult.getResult().stream().map(x->x.getCustomerCode()).collect(Collectors.toList());
                queryCustomerCodes.addAll(customerCodes);
            }
        }
        if (CollectionUtils.isEmpty(queryCustomerCodes)) {
            return Result.listEmptySuccess();
        }
        queryCustomerStatementReportRequest.setCustomerCodes(queryCustomerCodes);
        PageResult<List<CustomerStatementReportSDO>> pageResult = customerStatementReportReadRepository.pageQueryList(queryCustomerStatementReportRequest);

        return Result.listSuccess(pageResult.getContent(),pageResult.getTotal());
    }

}
